package com.jinsi.service.lv.iml;




import com.jinsi.constant.LhrConstant;
import com.jinsi.mapper.lv.SyLvCurriculumMapper;
import com.jinsi.pojo.SyLvCurriculum;
import com.jinsi.pojo.SyLvCurriculumExamineLog;
import com.jinsi.pojo.SyLvOperationLog;
import com.jinsi.pojo.SylvCurriculumLog;

import com.jinsi.service.lv.SyLvCourriculumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SyLvCourriculumServiceImpI implements SyLvCourriculumService {

    @Autowired
    private SyLvCurriculumMapper syLvCurriculumMapper;

    @Override
    @Transactional
    public List<SyLvCurriculum> getAllCm(SyLvCurriculum syLvCurriculum) {
        syLvCurriculum.setCurrculumIsActive(LhrConstant.CUT_OFF);
        return syLvCurriculumMapper.getAllCm(syLvCurriculum);

    }

    @Override
    @Transactional
    public int count(Integer curriculumUserid) {
        return syLvCurriculumMapper.count(curriculumUserid);
    }

    /**
     * 修改上架状态根据查到的状态修改取反
     * @param curriculumId
     * @param currculumUpdatedBy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOnShelfStatus(int curriculumId, String currculumUpdatedBy) {
        int getCS = syLvCurriculumMapper.getCS(curriculumId);
        if (getCS == 0){
            int curriculumShelves = 1;
            syLvCurriculumMapper.updateOnShelfStatus(curriculumId,curriculumShelves,currculumUpdatedBy);
        }else {
            int curriculumShelves = 0;
            syLvCurriculumMapper.updateOnShelfStatus(curriculumId,curriculumShelves,currculumUpdatedBy);
        }
    }

    /**
     * 修改课程信息时判断课程视频长度不足10分钟审核不通过
     * @param syLvCurriculum
     * @param videoDuration
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SyLvCurriculum syLvCurriculum,Integer videoDuration) {
        log.info("审核时长判断",videoDuration);
        boolean result = false;
        if(videoDuration < 600){
            log.info("视频时长不足10分钟",videoDuration);
            syLvCurriculum.setCurriculumShelves(1);
            syLvCurriculum.setCurriculumToExamine(2);
            syLvCurriculum.setCurriculumDuration(videoDuration);
            syLvCurriculum.setCurrculumUpdatedBy("系统修改");
            syLvCurriculumMapper.update(syLvCurriculum);
            SyLvCurriculumExamineLog syLvCurriculumExamineLog = new SyLvCurriculumExamineLog();
            syLvCurriculumExamineLog.setLogCurriculumPid(syLvCurriculum.getCurriculumId());
            syLvCurriculumExamineLog.setLogAuditDetails("视频时长过短：不应小于10分钟");
            Date date = new Date();
            Timestamp nousedate = new Timestamp(date.getTime());
            syLvCurriculumExamineLog.setLogAuditTime(nousedate);
            syLvCurriculumExamineLog.setLogReviewedBy("系统拦截");
            syLvCurriculumExamineLog.setLogResult(2);
            syLvCurriculumMapper.insertExamineLog(syLvCurriculumExamineLog);
        }else {
            log.info("视频时长大于10分钟",videoDuration);
            syLvCurriculum.setCurriculumShelves(0);
            syLvCurriculum.setCurriculumToExamine(0);
            syLvCurriculum.setCurriculumDuration(videoDuration);
            log.info("修改数据[开始]",syLvCurriculum);
            syLvCurriculumMapper.update(syLvCurriculum);
            result = true;
            log.info("修改数据[结束]",result);
        }

        SyLvCurriculum getid = syLvCurriculumMapper.getId(syLvCurriculum.getCurriculumId());
        SylvCurriculumLog sylvCurriculumLog = new SylvCurriculumLog();
        sylvCurriculumLog.setLogPid(getid.getCurriculumId());
        sylvCurriculumLog.setLogCourseName(getid.getCurriculumName());
        sylvCurriculumLog.setLogOriginalPrice(getid.getCurriculumOriginalPrice());
        sylvCurriculumLog.setLogPresentPrice(getid.getCurriculumPresentPrice());
        sylvCurriculumLog.setLogGrounding(getid.getCurriculumShelves());
        sylvCurriculumLog.setLogAuditStatus(getid.getCurriculumToExamine());
        sylvCurriculumLog.setLogModifiedBy(getid.getCurrculumUpdatedBy());
        sylvCurriculumLog.setLogSerialNumber(getid.getCurriculumSerialNumber());
        //获取一个Date对象
        Date date=new Date();
        //创建一个格式化日期对象
        DateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //格式化后的时间
        String birthday= simpleDateFormat.format(date);
        sylvCurriculumLog.setLogModifiedOn(birthday);
        log.info("添加日志数据[开始]",sylvCurriculumLog);
        syLvCurriculumMapper.add(sylvCurriculumLog);
        result = true;
        log.info("添加日志[完成]",result);
    }

    @Override
    @Transactional
    public List<SylvCurriculumLog> log(int curriculumId) {
        List<SylvCurriculumLog> log = syLvCurriculumMapper.log(curriculumId);
        return  log;
    }

    @Override
    @Transactional
    public void delete(int curriculumId) {
        syLvCurriculumMapper.delete(curriculumId,LhrConstant.DELETE);
    }

    /**
     * 添加课程信息根据视频长度做拦截不足10分钟审核不通过
     * @param syLvCurriculum
     * @param videoDuration
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(SyLvCurriculum syLvCurriculum,int videoDuration) {
        log.info("审核时长判断",videoDuration);
        if(videoDuration < 600){
            log.info("视频时长不足10分钟");
            syLvCurriculum.setCurriculumShelves(1);
            syLvCurriculum.setCurriculumToExamine(2);
            syLvCurriculum.setCurriculumDuration(videoDuration);
            syLvCurriculumMapper.insert(syLvCurriculum);
            SyLvCurriculumExamineLog syLvCurriculumExamineLog = new SyLvCurriculumExamineLog();
            syLvCurriculumExamineLog.setLogCurriculumPid(syLvCurriculum.getCurriculumId());
            syLvCurriculumExamineLog.setLogAuditDetails("视频时长过短：不应小于10分钟");
            Date date = new Date();
            Timestamp nousedate = new Timestamp(date.getTime());
            syLvCurriculumExamineLog.setLogAuditTime(nousedate);
            syLvCurriculumExamineLog.setLogReviewedBy("系统拦截");
            syLvCurriculumExamineLog.setLogResult(2);
            syLvCurriculumMapper.insertExamineLog(syLvCurriculumExamineLog);
        }else {
            log.info("视频时长大于10分钟",videoDuration);
            syLvCurriculum.setCurriculumShelves(0);
            syLvCurriculum.setCurriculumToExamine(0);
            syLvCurriculum.setCurriculumDuration(videoDuration);
            log.info("添加数据[开始]",syLvCurriculum);
            syLvCurriculumMapper.insert(syLvCurriculum);
            boolean result = true;
            log.info("添加数据[完成]",result);
        }

    }


    @Override
    @Transactional
    public List<SyLvCurriculumExamineLog> examineLog(int curriculumId) {
        List<SyLvCurriculumExamineLog> examinelog = syLvCurriculumMapper.examineLog(curriculumId);
        return examinelog;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditDetails(int curriculumId, int curriculumToExamine, String logAuditDetails, String uname) {
        SyLvCurriculum syLvCurriculum = new SyLvCurriculum();
        syLvCurriculum.setCurriculumId(curriculumId);
        syLvCurriculum.setCurriculumToExamine(curriculumToExamine);
        syLvCurriculum.setCurrculumUpdatedBy(uname);
        //获取一个Date对象
        Date date=new Date();
        //创建一个格式化日期对象
        DateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //格式化后的时间
        String birthday= simpleDateFormat.format(date);
        syLvCurriculum.setCurrculumUpdateTime(birthday);
        log.info("修改更新人信息[开始]",syLvCurriculum);
        syLvCurriculumMapper.auditDetailsupdate(syLvCurriculum);
        boolean result = true;
        log.info("修改更新人信息[结束]",result);
        SyLvCurriculumExamineLog syLvCurriculumExamineLog = new SyLvCurriculumExamineLog();
        syLvCurriculumExamineLog.setLogCurriculumPid(curriculumId);
        syLvCurriculumExamineLog.setLogAuditDetails(logAuditDetails);
        Date date1 = new Date();
        Timestamp nousedate = new Timestamp(date1.getTime());
        syLvCurriculumExamineLog.setLogAuditTime(nousedate);
        syLvCurriculumExamineLog.setLogReviewedBy(uname);
        syLvCurriculumExamineLog.setLogResult(curriculumToExamine);
        log.info("添加审核日志[开始]",syLvCurriculumExamineLog);
        syLvCurriculumMapper.auditDetailsinset(syLvCurriculumExamineLog);
        result = true;
        log.info("添加审核日志[结束]",result);
    }

    @Override
    @Transactional
    public List<SyLvCurriculumExamineLog> auditLog() {
        List<SyLvCurriculumExamineLog> syLvCurriculumExamineLogs = syLvCurriculumMapper.auditLog();
        return syLvCurriculumExamineLogs;
    }

    @Override
    @Transactional
    public void addOperationLog(SyLvOperationLog syLvOperationLog) {
        Date date1 = new Date();
        Timestamp nousedate = new Timestamp(date1.getTime());
        syLvOperationLog.setLogDate(nousedate);
        syLvCurriculumMapper.addOperationLog(syLvOperationLog);
    }

    @Override
    @Transactional
    public List<SyLvOperationLog> operationLog() {
        return syLvCurriculumMapper.operationLog(LhrConstant.CUT_OFF);
    }

    @Override
    @Transactional
    public void deloperationLog(int logId) {
        syLvCurriculumMapper.deloperationLog(logId,LhrConstant.DELETE);
    }
}
